package application

import (
	"invitation/internal/config"
	"invitation/internal/pkg/ai/zpqy"
	"invitation/internal/repository"
	"invitation/internal/repository/cache"
	"time"
)

type Application struct {
	applicationConfig *config.ApplicationConfig
	dbLoader          config.DBLoader
	repoManager       *repository.RepoManager
	zpqy              *zpqy.Client
}

func newApplication(ApplicationConfig *config.ApplicationConfig) *Application {
	dbLoader := config.NewSqliteDBLoader(ApplicationConfig.DBFilePath)
	dbLoader.Load()
	repoManager := repository.NewRepoManager(dbLoader.DB(), cache.New(2000))
	zpqyCfg := zpqy.Config{
		BaseURL:     ApplicationConfig.LLMBaseUrl,
		APIKey:      ApplicationConfig.LLMApiKey,
		ModelName:   ApplicationConfig.LLMModelName,
		HTTPTimeout: 120 * time.Second,
		MaxRetries:  5,
		RetryDelay:  2 * time.Second,
	}
	return &Application{
		applicationConfig: ApplicationConfig,
		dbLoader:          dbLoader,
		repoManager:       repoManager,
		zpqy:              zpqy.NewClient(zpqyCfg),
	}
}

func (a *Application) ApplicationConfig() *config.ApplicationConfig {
	return a.applicationConfig
}

func (a *Application) RepoManager() *repository.RepoManager {
	return a.repoManager
}

func (a *Application) ZhiPuQingYan() *zpqy.Client {
	return a.zpqy
}
